package com.free.semantic.infrastruct.repository.impl;

import com.free.semantic.infrastruct.common.RelationItemDO;
import com.free.semantic.infrastruct.mapper.RelationItemMapper;
import com.free.semantic.infrastruct.repository.RelationItemRepository;
import com.free.semantic.model.conceptmodel.RelationItemEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

@Repository
public class RelationItemRepositoryImpl implements RelationItemRepository {

    @Autowired
    private RelationItemMapper relationItemMapper;

    @Override
    public RelationItemEntity findById(Long id) {
        RelationItemDO itemDO = relationItemMapper.findById(id);
        return convertToModel(itemDO);
    }

    @Override
    public List<RelationItemEntity> findBySemanticNetworkId(Long semanticNetworkId) {
        List<RelationItemDO> itemDOs = relationItemMapper.findBySemanticNetworkId(semanticNetworkId);
        return itemDOs.stream()
                .map(this::convertToModel)
                .collect(Collectors.toList());
    }

    @Override
    public Long save(RelationItemEntity relationItemEntity) {
        RelationItemDO itemDO = convertToDO(relationItemEntity);
        relationItemMapper.insert(itemDO);
        return itemDO.getId();
    }

    @Override
    public void update(RelationItemEntity relationItemEntity) {
        RelationItemDO itemDO = convertToDO(relationItemEntity);
        relationItemMapper.update(itemDO);
    }

    @Override
    public void deleteById(Long id) {
        relationItemMapper.deleteById(id);
    }

    @Override
    public void deleteBySemanticNetworkId(Long semanticNetworkId) {
        relationItemMapper.deleteBySemanticNetworkId(semanticNetworkId);
    }

    private RelationItemEntity convertToModel(RelationItemDO itemDO) {
        if (itemDO == null) {
            return null;
        }
        RelationItemEntity item = new RelationItemEntity();
        item.setId(itemDO.getId());
        item.setName(itemDO.getName());
        item.setVersion(itemDO.getVersion());
        item.setSemanticNetworkId(itemDO.getSemanticNetworkId());
        item.setRelationStr(itemDO.getRelationStr());
        return item;
    }

    private RelationItemDO convertToDO(RelationItemEntity item) {
        if (item == null) {
            return null;
        }
        RelationItemDO itemDO = new RelationItemDO();
        itemDO.setId(item.getId());
        itemDO.setName(item.getName());
        itemDO.setVersion(item.getVersion());
        itemDO.setSemanticNetworkId(item.getSemanticNetworkId());
        itemDO.setRelationStr(item.getRelationStr());
        return itemDO;
    }

    private RelationItemEntity convertToEntity(RelationItemDO relationItemDO) {
        if (relationItemDO == null) {
            return null;
        }
        RelationItemEntity entity = new RelationItemEntity();
        entity.setId(relationItemDO.getId());
        entity.setName(relationItemDO.getName());
        entity.setVersion(relationItemDO.getVersion());
        entity.setSemanticNetworkId(relationItemDO.getSemanticNetworkId());
        // Note: sourceObjects and targetObject need to be populated separately
        return entity;
    }
} 